import { post } from '../../../../utils/request.js'
import { isCardNo ,isPhoneNo} from '../../../../utils/util.js'
import global from '../../../../utils/global.js'
let that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '请选择初始日期 ›',
    userId: wx.getStorageSync("useInfo").id,
    isEdit:false,
    id:"",
    options:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    that.setData({
      options: options
    })
   

  },
  onShow:function(){
    let options = that.data.options
    if (options.item != "1") {
      let dataItem = JSON.parse(options.item)
      if (dataItem.sex == 1) {
        that.setData({
          isMan: false
        })
      } else if (dataItem.sex == 0) {
        that.setData({
          isMan: true
        })
      }
      let data = {
        name: dataItem.name,
        phone: dataItem.phone,
        identity: dataItem.identity,
        sex: dataItem.sex
      }
      that.setData({
        data: data,
        isEdit: true,
        id: dataItem.id
      })
    }
  },
// 时间选择器
  bindDateChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  formSubmit: function (e) {
    // console.log(e.detail.value)
    let data = e.detail.value;
    let myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    let pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    that.setData({
      data: data
    })
    if (!myreg.test(data.phone)){
      wx.showModal({
        title: '提示',
        content: '手机号不合法，请重新输入',
      })
      return
    }
    if (!pattern.test(data.identity)) {
      wx.showModal({
        title: '提示',
        content: '身份证不合法',
      })
      return
    }
    if (!data.name){
      wx.showModal({
        title: '提示',
        content: '姓名不能为空',
      })
      return
    }
    if (!data.phone) {
      wx.showModal({
        title: '提示',
        content: '手机不能为空',
      })
      return
    }
    if (!data.identity) {
      wx.showModal({
        title: '提示',
        content: '身份证不能为空',
      })
      return
    }
    if (!data.sex) {
      wx.showModal({
        title: '提示',
        content: '请选择您的性别',
      })
      return
    }
    // userId:that.data.userId
    data.userId = that.data.userId;
    // data.userId="1738";
    if(!that.data.isEdit){
      that.insertUser(data)
    }else{
      data.id = that.data.id
      that.updateUser(data)
    }
    
  },
  // 新增insertUser
  insertUser:function(param){
    // console.log(param)
    post(global.apiUrl.insertUser, param, function (res) {
      // console.log(res.msg)
      if (res.msg =="操作成功!"){
        wx.showToast({
          title: '成功,正在返回上一页',
          icon: 'succes',
          duration: 3000,
          mask: true
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
        

      }else{
        wx.showModal({
          title: '提示',
          content: res.msg,
        })
      }
      if (getCurrentPages().length != 0) {
        //刷新当前页面的数据
        getCurrentPages()[getCurrentPages().length - 1].onLoad()
      }
      
    })
   
  },
  updateUser:function(param){
    // console.log(param)
    post(global.apiUrl.updateUser, param, function (res) {
      wx.showToast({
        title: res.msg,
        icon: 'succes',
        duration: 3000,
        mask: true
      })
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        })
      }, 2000)
      if (getCurrentPages().length != 0) {
        //刷新当前页面的数据
        getCurrentPages()[getCurrentPages().length - 1].onLoad()
      }
    })
  }
})